489B - BerSU Ball - CodeForces Solution


dfs and similar dp graph matchings greedy sortings two pointers *1200

Please click on ads to support us..

Python Code:

n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
m = int(input())
b = list(map(int, input().split()))
b.sort(reverse=True)
pointer_a = 0
pointer_b = 0
result = 0
while((pointer_a < n) and (pointer_b < m)):
    if (abs(a[pointer_a] - b[pointer_b]) <= 1):
        result += 1
        pointer_a += 1
        pointer_b += 1
    elif (a[pointer_a] > b[pointer_b]):
        pointer_a += 1
    else:
        pointer_b += 1

print(result)

C++ Code:

#include<bits/stdc++.h>
#include<cmath>
#include<iomanip>
#define pi 3.1415926535
#define MOD 1000000007
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define ll long long int
#define ld long double
#define N endl
using namespace std;


void danial(){

ll n;
cin>>n;
ll a[n];
for(ll i=0;i<n;i++){
    cin>>a[i];
}
sort(a,a+n);
ll m;
cin>>m;
ll b[m];
for(ll i=0;i<m;i++){
    cin>>b[i];
}
sort(b,b+m);
ll c=0;

    for(ll j=0;j<n;j++){

        for(ll k=0;k<m;k++){

        
     if((abs(a[j]-b[k]))<=1){
        c++;
        b[k]=2222;
        break;
     }
    }
    }
    cout<<c<<N;


}






int main(){

danial();
//}
}


Comments

Submit
0 Comments
More Questions

1613B - Absent Remainder
1536B - Prinzessin der Verurteilung
1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats
1703A - YES or YES
494A - Treasure
48B - Land Lot
835A - Key races
1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64
755B - PolandBall and Game
808B - Average Sleep Time
1515E - Phoenix and Computers
1552B - Running for Gold
994A - Fingerprints
1221C - Perfect Team
1709C - Recover an RBS
378A - Playing with Dice
248B - Chilly Willy
1709B - Also Try Minecraft
1418A - Buying Torches
131C - The World is a Theatre
1696A - NIT orz
1178D - Prime Graph
1711D - Rain
534A - Exam